package server

import (
	"gorm.io/gorm"
	"group_server/basic/config"
	pb "group_server/proto"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(30);not null;unique;comment:'用户名称'"`
	Password string `gorm:"type:char(32);not null;comment:'密码'"`
}

type Good struct {
	gorm.Model
	Name    string  `gorm:"type:varchar(50);not null;comment:'商品名称'"`
	Price   float64 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	Num     int     `gorm:"type:int(10);not null;comment:'商品库存'"`
	IsGroup int8    `gorm:"type:tinyint(1);comment:'是否为拼团:0-否,1-是'"`
}

type Order struct {
	gorm.Model
	UserId  int     `gorm:"type:int(10);not null;comment:'用户id'"`
	GoodId  int     `gorm:"type:int(10);not null;comment:'商品id'"`
	OrderSn string  `gorm:"type:char(36);not null;comment:'订单编号'"`
	Price   float64 `gorm:"type:decimal(10,2);comment:'订单价格'"`
	Num     int     `gorm:"type:int(10);comment:'订单数量'"`
	Status  int8    `gorm:"type:tinyint(1);comment:'订单状态:0-代付款,1-已付款,2-已完成,3-已取消'"`
}

func (u User) UserGet(id int64) (uint, string) {
	var user User
	err := config.DB.Where("id=?", id).First(&user).Error
	if err != nil {
		return 0, "用户不存在"
	}
	return user.ID, ""
}

func (g Good) GetGoodList() ([]*pb.GroupList, string) {
	var Lists []*pb.GroupList
	var goodLists []Good
	err := config.DB.Where("is_group=1").Find(&goodLists).Error
	for _, v := range goodLists {
		List := pb.GroupList{
			Name:  v.Name,
			Price: float32(v.Price),
			Num:   int64(v.Num),
		}
		Lists = append(Lists, &List)
	}
	if err != nil {
		return Lists, "获取失败"
	}
	return Lists, ""
}
